<template>
  <div>
    <!-- 基础信息 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>物流信息</span>
      </div>
      <div class="form-container">
        <el-form ref="logistocsForm" :model="firstForm" :rules="rules" label-width="100px" class="demo-ruleForm" size="small">
          <el-form-item label="运费模板：" prop="template_id">
            <el-select v-model="firstForm.template_id" filterable placeholder="默认运费模板" style="width: 20%; margin-right: 10px">
              <el-option
                v-for="(item,index) in freightList"
                :key="index"
                :label="item.template_name"
                :value="item.template_id"/>
            </el-select>
            <el-button size="small" @click="refresh()">刷新</el-button>
            <router-link :to= "{ path: '/freight-list'}" tag="a" target="_blank" class="el-button el-button--primary el-button--small">新建运费模板</router-link>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
export default {
  mixins: [],
  props: {
    freight: {
      type: Array,
      required: true
    },
    form: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      freightList: [],
      firstForm: {
        template_id: ''
      },
      rules: {
        template_id: [
          { required: true, message: '请选择运费模板', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    freight(value) {
      // 运费模板
      this.freightList = value
    },
    form(value) {
      this.firstForm = value
      this.firstForm.result = false
    }
  },
  created() {
    if (Cookies.get('template_id') !== 'undefined' && Cookies.get('template_id') !== null && Cookies.get('template_id') !== '') {
      this.firstForm.template_id = Cookies.get('template_id')
    }
  },
  methods: {
    // 给父组件传递收取表单的值
    sendLogisticsMsg() {
      this.$refs['logistocsForm'].validate((valid) => {
        if (valid) {
          this.firstForm.result = true
        }
        this.$emit('msgEvent', this.firstForm)
      })
    },
    // 刷新运费模版
    refresh() {
      // 监听父组件方法，刷新运费模版
      this.$emit('getFreightList')
    },
    toFreight() {
      // 跳转至商城配置-》运费模板页面
      const query = { 'status_id': '3' }
      this.$router.push({ path: '/shop/toIndex', query: query })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .form-container {
    width: 90%;
  }
  .box-card {
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .el-card__body {
    padding: 20px 20px 0 20px!important;
  }
  .form-container-parameter {
    background: #f3f3f3;
    padding: 20px 20px 0 0;
  }
</style>
